import 'package:flutter/material.dart';
import 'package:flutter_module/beans/data_layer_item_bean.dart';
import 'package:flutter_module/system/log_util.dart';
import 'package:flutter_module/viewModels/layer_edit_viewmodel.dart';
import 'package:flutter_module/views/layerItemWidgets/single_selection_widget.dart';
import 'package:flutter_module/views/fragments/layer_item_type_list_fragment.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';

import '../layerItemWidgets/default_Input_widget.dart';

///数据模板字段编辑页面
class DataLayerItemEditFragment extends StatefulWidget {
  final DataLayerItem? item;

  const DataLayerItemEditFragment({this.item, Key? key}) : super(key: key);

  @override
  State<DataLayerItemEditFragment> createState() =>
      _DataLayerItemEditFragmentState();
}

class _DataLayerItemEditFragmentState extends State<DataLayerItemEditFragment> {
  late final NavigatorState _navi;
  late final LayerEditViewModel _read;

  late final TextEditingController _nameController;

  late final TextEditingController _describeController;

  @override
  void initState() {
    _nameController = TextEditingController()
      ..addListener(() {
        _read.curItem.title = _nameController.text;
      });

    _describeController = TextEditingController()
      ..addListener(() {
        _read.curItem.describe = _describeController.text;
      });
    super.initState();
  }

  @override
  void didChangeDependencies() {
    _navi = Navigator.of(context);
    _read = Provider.of<LayerEditViewModel>(context, listen: false);
    _read.initLayerItem(item: widget.item);
    _nameController.text = _read.curItem.title;
    _describeController.text = "${_read.curItem.describe}";
    super.didChangeDependencies();
  }

  @override
  void dispose() {
    _nameController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    LogUtil.instance.printf("图层参数数据编辑 build");
    return Scaffold(
      backgroundColor: Colors.grey,
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        leading: BackButton(
          onPressed: () {
            _navi.pop("back");
          },
        ),
        actions: [
          TextButton(
            onPressed: () {
              if (_read.saveItem()) {
                _navi.pop("addItem");
              }
            },
            child: const Text(
              '保存',
              style: TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
      body: ListView.builder(
        itemCount: 5,
        itemBuilder: (BuildContext context, int index) {
          switch (index) {
            case 0:
              return TextField(
                controller: _nameController,
                decoration: InputDecoration(
                  labelText: '属性名称: ',
                  hintText: "请输入属性名称",
                  labelStyle: const TextStyle(color: Colors.red),
                  icon: const Icon(Icons.insert_drive_file),
                  suffixIcon: IconButton(
                    tooltip: '清空',
                    icon: const Icon(Icons.clear),
                    onPressed: () {
                      _nameController.text = "";
                    },
                  ),
                ),
              );
            case 1:
              return Card(
                child: Selector<LayerEditViewModel, DataLayerItemType>(
                    builder: (context, value, child) {
                      return ListTile(
                        title: Text(LayerItemConstant.getTypeName(value)),
                        leading: LayerItemConstant.getTypeIcon(value),
                        subtitle: const Text("请选择类型"),
                        trailing: const Icon(Icons.arrow_forward_ios),
                        onTap: () {
                          _navi.push(MaterialPageRoute(
                              builder: (BuildContext context) {
                            return const LayerItemTypeListFragment();
                          }));
                        },
                      );
                    },
                    selector: (context, model) => model.curItem.type),
              );
            case 2:
              return Selector<LayerEditViewModel, DataLayerItemType>(
                  builder: (context, value, child) {
                    switch (value) {
                      case DataLayerItemType.dataLayerItemTypeInput:
                        return LayerItemInput(_read);
                      case DataLayerItemType.dataLayerItemTypeSingleSelection:
                        return const SingleSelection();
                    }
                    return Container();
                  },
                  selector: (context, model) => model.curItem.type);
            case 3:
              return const Text("备注");
            case 4:
              return TextFormField(
                controller: _describeController,
                autofocus: false,
                autocorrect: false,
                keyboardType: TextInputType.multiline,
                minLines: 8,
                maxLines: 8,
                decoration: const InputDecoration(
                  filled: true,
                  hintText: "可输入描述信息",
                  fillColor: Color(0xFFF2F2F2),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    borderSide: BorderSide(width: 1),
                  ),
                ),
              );
            default:
              return Container();
          }
        },
      ),
    );
  }
}
